/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.painting.resources;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

import com.hercules.android.receiver.LogUtil;
import com.hercules.android.receiver.model.objects.XRENineSliceImage;
import com.hercules.android.receiver.painting.PaintersUtil;
import com.hercules.android.receiver.painting.XREObjectPainterException;

/**
 * Painter for {@link XRENineSliceImage} resource. First it will load image from URL and then paint it on screen divide
 * to nine parts.
 * <p>
 * AndroidAssembly1 this class was added in Android Assembly 1
 * <p>
 * <b>Thread safety:</b> Not thread safe, because base class is not thread safe
 *
 * @author 7realm
 * @version 1.0
 */
public class XRENineSliceImagePainter extends XREImagePainter {
    /**
     * Constructor for class {@link XRENineSliceImagePainter}.
     *
     * @param xreResource the {@link XRENineSliceImage}
     */
    public XRENineSliceImagePainter(XRENineSliceImage xreResource) {
        super(xreResource);
    }

    /**
     * Draw resource on surface.
     *
     * @param canvas the canvas to be used
     */
    @Override
    public void onDraw(Canvas canvas) {
        try {
            // get the image
            Bitmap image = PaintersUtil.getBitmap(getImageUrl());
            if (image != null) {
                XRENineSliceImage sliceImage = (XRENineSliceImage) getXREResource();

                // calculate the region to draw according to resource parameters
                int width = image.getWidth();
                int height = image.getHeight();
                Rect r = PaintersUtil.calculateStretchAndAling(getXREView(), width, height);

                // Refer to http://help.adobe.com/en_US/Fireworks/9.0/help.html?content=frw_styles_st_13.html
                // for detailed information about drawing of nine slice image
                int sl = sliceImage.getLeft();
                int st = sliceImage.getTop();
                int sliceR = sliceImage.getRight();
                int sliceB = sliceImage.getBottom();
                int sr = width - sliceR;
                int sb = height - sliceB;

                // draw the corners, top-left, top-right, bottom-right, bottom-left
                canvas
                    .drawBitmap(image, new Rect(0, 0, sl, st), new Rect(r.left, r.top, r.left + sl, r.top + st), null);
                canvas.drawBitmap(image, new Rect(sr, 0, width, st), new Rect(r.right - sliceR, r.top, r.right, r.top
                    + st), null);
                canvas.drawBitmap(image, new Rect(sr, sb, width, height), new Rect(r.right - sliceR, r.bottom - sliceB,
                    r.right, r.bottom), null);
                canvas.drawBitmap(image, new Rect(0, sb, sl, height), new Rect(0, r.bottom - sliceB, sl, r.bottom),
                    null);

                // draw the borders, top, right, bottom, left
                canvas.drawBitmap(image, new Rect(sl, 0, sr, st), new Rect(r.left + sl, r.top, r.right - sliceR, r.top
                    + st), null);
                canvas.drawBitmap(image, new Rect(sr, st, width, sb), new Rect(r.right - sliceR, r.top + st, r.right,
                    r.bottom - sliceB), null);
                canvas.drawBitmap(image, new Rect(sl, sb, sr, height), new Rect(r.left + sl, r.bottom - sliceB, r.right
                    - sliceR, r.bottom), null);
                canvas.drawBitmap(image, new Rect(0, st, sl, sb), new Rect(r.left, r.top + st, r.left + sl, r.bottom
                    - sliceB), null);

                // draw the center region
                canvas.drawBitmap(image, new Rect(sl, st, sr, sb), new Rect(r.left + sl, r.top + st, r.right - sliceR,
                    r.bottom - sliceB), null);
            }
        } catch (XREObjectPainterException e) {
            LogUtil.error(getLogger(), this, "onDraw", e);
        }
    }
}
